package com.qin.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.qin.entity.Book;
import com.qin.service.BookService;
import com.qin.service.impl.BookServiceImpl;

/**
 * 根据bookid查询库存
 */
@WebServlet("/getstock")
public class GetStockServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private BookService bookService = new BookServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String parameter = request.getParameter("bookId");
		// 如果是数字
		if(StringUtils.isNumber(parameter)) {
			// 拿到id
			long parseLong = Long.parseLong(parameter);
			Book bookById = bookService.getBookById(parseLong);
			
			// 把库存响应出去
			response.getWriter().print(bookById.getStock());
		}else {
			response.getWriter().print(false);
		}
		
	}

}
